#include "video.h"
#include "ui_video.h"
#include "QDebug"
#include "QTime"
#include "QFileDialog"
#include <QThread>
video::video(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::video)
{
    ui->setupUi(this);
    myprocess.setProcessChannelMode(QProcess::MergedChannels);
    connect(&myprocess,&QProcess::readyReadStandardOutput,this,&video::read_process_output);
    connect(&mytimer,&QTimer::timeout,this,&video::get_video_now_time);
    flag = false;
}

video::~video()
{
    delete ui;
}


void video::on_calc_clicked()
{
    QString path = ui->listWidget->currentItem()->text();
//    QString path = QString("F:/destok/Sitelog/qt/workplace/avi");
    QString cmd = QString("mplayer -quiet -slave %1 -wid  %2").arg(path).arg(ui->label->winId());
    myprocess.start(cmd);
    myprocess.write("get_time_length\n");
    mytimer.start(1000);
//    myprocess.start("calc");
//    qDebug()<<myprocess.state();


//    myprocess.kill();
//    myprocess.waitForFinished();
}
void video::read_process_output()
{
    QString arr = myprocess.readAll();
    qDebug()<<arr;
    if(arr.contains("ANS_LENGTH"))
    {
        arr.remove("ANS_LENGTH=");
        arr.remove("\r\n");
        qDebug()<<arr;
//        total_times = (int)arr.toFloat();
        double a = arr.toFloat();
        total_times = static_cast<int>(a);
        QTime total_t(total_times/3600,total_times/60%60,total_times%60);
        QString ti = total_t.toString("hh::mm::ss");
        ui->total_times->setText(ti);
        ui->horizontalSlider->setMaximum(total_times);
        ui->horizontalSlider->setMinimum(0);
    }
    if(arr.contains("ANS_TIME_POSITION"))
    {
        arr.remove("ANS_TIME_POSITION=");
        arr.remove("\r\n");

        qDebug()<<arr;
        double a = arr.toFloat();
        now_times = static_cast<int>(a);
        QTime now_times_t(now_times/3600,now_times/60%60,now_times%60);
        QString ti = now_times_t.toString("hh::mm::ss");
        ui->now_time->setText(ti);
        if(!flag)
        {
            ui->horizontalSlider->setValue(now_times);
        }

    }
}

void video::get_video_now_time()
{
    myprocess.write("get_time_pos\n");
    if(myprocess.state()==QProcess::NotRunning)
    {
        video::on_down_clicked();
    }
}

void video::on_up_clicked()
{
    int i = ui->listWidget->currentRow();
    i--;
    if(i==-1)
    {
        i = ui->listWidget->count()-1;
    }
    ui->listWidget->setCurrentRow(i);
    QString path = ui->listWidget->currentItem()->text();
    if(myprocess.state() ==QProcess::Running)
    {
        myprocess.kill();
        myprocess.waitForFinished();

    }
    QString cmd = QString("mplayer -quiet -slave %1 -wid  %2").arg(path).arg(ui->label->winId());
    myprocess.start(cmd);
}

void video::on_down_clicked()
{
    int i = ui->listWidget->currentRow();
    i++;
    if(i==ui->listWidget->count())
    {
        i = 0;
    }
    ui->listWidget->setCurrentRow(i);
    QString path = ui->listWidget->currentItem()->text();
    if(myprocess.state() ==QProcess::Running)
    {
        myprocess.kill();
        myprocess.waitForFinished();

    }
    QString cmd = QString("mplayer -quiet -slave %1 -wid  %2").arg(path).arg(ui->label->winId());
    myprocess.start(cmd);
}

void video::on_open_clicked()
{
    QString file_path = QFileDialog::getOpenFileName(this,"选择文件","../../avi/","*.avi");
    ui->listWidget->addItem(file_path);
}

void video::on_horizontalSlider_sliderMoved(int position)
{
    time_move_at = position;
    //移动的过程
    //此时不允许在定时器在移动我们的进度条
    flag =  true;
}

void video::on_horizontalSlider_sliderReleased()
{
    int data = time_move_at - now_times;
    QString cmd = QString("seek %1\n").arg(data);
    myprocess.write(cmd.toUtf8());
    flag = false;
}

void video::on_back_clicked()
{
    myprocess.kill();
    myprocess.waitForFinished();
    mytimer.stop();
    this->parentWidget()->show();
    this->hide();

}
